package com.physique.tea.Service.Impl;

import com.physique.tea.Global.Result;
import com.physique.tea.Global.ResultList;
import com.physique.tea.Mapper.UserMapper;
import com.physique.tea.Pojo.User;
import com.physique.tea.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Result login(User user) {

        String username = user.getUSERNAME();
        String password = user.getPASSWORD();
        //
        User user1 = userMapper.selectByUsername(username);

        if(user1 == null|| !Objects.equals(user1.getPASSWORD(), password)){
            return new Result(ResultList.PASSWORD_WRONG.getCode(),
                    ResultList.PASSWORD_WRONG.getMessage());
        }


        return new Result(ResultList.SUCCESS.getCode(), ResultList.SUCCESS.getMessage());
    }

    @Override
    public Result register(User user) {
        String username = user.getUSERNAME();
        String password = user.getPASSWORD();

        User user1 = userMapper.selectByUsername(username);
        if(user1!=null){
            return new Result(ResultList.USER_EXIST.getCode(),
                    ResultList.USER_EXIST.getMessage());
        }
        userMapper.insertUser(user);
        return new Result(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage());
    }
}
